from sqlalchemy import Table, Column, BigInteger, String


def tableDataFromJson(data):
    tableData = data['data']
    tableData['timestamp'] = data['timestamp']
    return tableData


def tableObjectFromJson(data, metadata):

    tableName = data['type']
    tableData = data['data']
    tableData['timestamp'] = data['timestamp']

    colList = []
    for key in tableData.keys():
        value = tableData[key]
        typeStr = ''
        if(type(value).__name__ == 'str'):
            typeStr = String
        elif(type(value).__name__ == 'int'):
            typeStr = BigInteger
        if(typeStr != ''):
            # Column('time',Integer)
            col = Column(key, typeStr)
            colList.append(col)
    table = Table(tableName, metadata, *colList)
    return table


def insertTable(table, data, conn):
    ins = table.insert()
    ins = ins.values(**data)
    conn.execute(ins)


def createTable(eng, metadata):
    metadata.create_all(eng)
